package com.hopool.shop.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class SessionUtil {
	protected static SessionFactory sf;
	protected static ThreadLocal<Session> tl =  new ThreadLocal<Session>();
	static{
		Configuration cfg = new Configuration().configure();
		sf = cfg.buildSessionFactory();
	}
	public SessionFactory getSf() {
		return sf;
	}
	public static Session getCurrentSession(){
		Session s = tl.get();
		if(s==null){
			s = sf.openSession();
			tl.set(s);
		}
		return s;
	}
	
	public static void closeSession(){
		tl.get().close();
	}
	
	public static void closeSessionFactory(){
		sf.close();
	}
}
